package com.service.khqy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOL;
import com.bpms.common.VOList;
import com.bpms.esb.IService;

public class Addcontract implements IService {
	
	public VO exec(VO arg0) {
		JdbcWorker j = null;
		VO vo = new VO();
		try {
			j = new JdbcWorker();
			j.startTransaction();
			try {
				String idtype=arg0.getAttr("idtype");
				if ("身份证".equals(idtype)) {
					vo.setAttr("idtype", "0"); //证件类型
				} else if ("护照".equals(idtype)) {
					vo.setAttr("idtype", "1");
				} else {
					vo.setAttr("idtype", "2");
				}
				
				vo.setAttr("name", arg0.getAttr("name"));// 户名
				vo.setAttr("idcard", arg0.getAttr("idcard"));// 证件号码
				vo.setAttr("mobile", arg0.getAttr("mobile"));// 手机号
				vo.setAttr("address",arg0.getAttr("address"));//联系人地址
				vo.setAttr("CREATEDATE",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
				vo.setAttr("CREATEUSER",arg0.getAttr("CREATEUSER"));
				
				vo.voType = "ACCOUNTINFO";
				
				if ("update".equals(arg0.getAttr("type"))) {
					VO k = new VO();
					k.voType = "ACCOUNTINFO";
					k.keyString = "ID";
					k.setAttr("id",arg0.getAttr("id"));
					
					String available=arg0.getAttr("available");
					if("不可用".equals(available)){
						vo.setAttr("available","0");//是否可用
					}else if("可用".equals(available)){
						vo.setAttr("available","1");
					}else if("已注销".equals(available)){
						vo.setAttr("available","2");
					}
					j.update(vo,k);
					
					String id=arg0.getAttr("id");
					String fullname=arg0.getAttr("fullname");
					String bank=arg0.getAttr("bank");
					int n=Integer.parseInt(arg0.getAttr("n"));
					for (int i = 0; i < n; i++) {
						
						String account=arg0.getAttr("account0"+i);
						StringBuffer sql=new StringBuffer("select ACCOUNT from ACCOUNTNO WHERE ACCOUNTID='").append(id).append("'");
						VOList volist = j.readAll(new VO(), sql.toString());
						VOL vol = new VOL(volist);
						Vector<VO> vector = vol.listData;
						
						boolean msg=true;
						for (VO vo2 : vector) {
							if(account.equals(vo2.getAttr("ACCOUNT"))){
								//做更新操作
								VO v1 = new VO();
								v1.setAttr("AVAILABLE", arg0.getAttr("available0")+i);
								v1.setAttr("FULLNAME",fullname);
								v1.setAttr("BANK",bank);
								
								VO k1 = new VO();
								k1.voType = "ACCOUNTNO";
								k1.keyString = "ACCOUNTID";
								k1.keyString = "ACCOUNT";
								k1.setAttr("ACCOUNTID",id);
								k1.setAttr("ACCOUNT",account);
								
								j.update(v1,k1);
								msg=false;
								break;
							}
						}
						if(msg){
							//做插入操作
							VO v1 = new VO();
							v1.voType="ACCOUNTNO";
							v1.setAttr("ACCOUNTID",id);
							v1.setAttr("ACCOUNT",account);
							v1.setAttr("FULLNAME",fullname);
							v1.setAttr("BANK",bank);
							System.out.println("AVAILABLE..."+arg0.getAttr("available0")+i);
							v1.setAttr("AVAILABLE", arg0.getAttr("available0")+i);
							
							j.insert(v1);
						}
					}//外层循环结束
				} else {//update end
					vo.setAttr("iccard", arg0.getAttr("iccard"));// ic卡
					vo.setAttr("password", arg0.getAttr("password"));// 密码
					String id = System.currentTimeMillis() + "";
					vo.setAttr("id", id);//签约账户编号

					j.insert(vo);//增加签约账户
					
					int n = Integer.parseInt(arg0.getAttr("j"));
					String fullname=arg0.getAttr("fullname0");
					String bank=arg0.getAttr("bank0");
					for (int i = 0; i < n; i++) {
						VO v = new VO();
						v.setAttr("ACCOUNTID", id);// 户名
						v.setAttr("ACCOUNT", arg0.getAttr("account" + i));
						v.setAttr("FULLNAME",fullname);
						v.setAttr("BANK",bank);

						v.voType = "ACCOUNTNO";

						j.insert(v);
					}
				}

				j.commit();
				if("update".equals(arg0.getAttr("type"))){
					vo.setAttr("result", "1");
					vo.setAttr("msg", "修改客戶簽約成功");
				}else{
					vo.setAttr("result", "1");
					vo.setAttr("msg", "客戶簽約成功");
				}

			} catch (Exception e) {
				CommonUtil.debug("Getcontract exception", e);
				vo.setAttr("result", "9999");
				vo.setAttr("msg", "获得签约错误");
				j.rollback();
			}

		} catch (Exception e) {
			CommonUtil.debug("Getcontract exception", e);
			vo.setAttr("result", "9999");
			vo.setAttr("msg", "获得签约错误");
		}
		return vo;
	}

//	private boolean verifyAccount() {
//		String sql="select ACCOUNT from ACCOUNTNO WHERE AVAILABLE='1'";
//		VOList volist = j.readAll(new VO(), sql);
//		VOL vol = new VOL(volist);
//		Vector<VO> vector = vol.listData;
//	}


}
